<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/1
 * Time: 11:03
 */
namespace app\admin\controller;
use think\Db;
class Media extends Base {

    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.media_title'] = ['like',"%" . $keyword . "%"];
            }
            $map['is_publish'] = 1;
            $count = Db::table('up_media')->alias('a')
                ->join('up_admin ua','ua.id = a.media_cz_user','left')
                ->where($map)
                ->count();
            $list = Db::table('up_media')->alias('a')
                ->join('up_admin ua','ua.id = a.media_cz_user','left')
                ->where($map)
                ->field('a.*')
                ->order('a.media_id desc')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                if($vo['is_admin']==1){//前台用户发布
                    $user_username1 = Db::table('up_u_user')->where('user_id',$vo['media_user_id'])->value('user_username');
                }elseif ($vo['is_admin']==2){//后台发布
                    $user_username1 = Db::table('up_admin')->where('id',$vo['media_user_id'])->value('username');
                }
                $vo['user_username1'] = $user_username1;
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }
    //删除
    public function del(){
        $id  = input('param.id');
        $where['type'] = 2;
        $where['f_id'] = $id;
        $info = Db::table('up_comment')->where($where)->count();
        $info1 = Db::table('up_comment')->where($where)->select();
        if($info>0){
            foreach ($info1 as $key=>&$vo){
                $where1['f_id'] = $vo['comment_id'];
                $count1 = Db::table('up_comment_reply')->where($where1)->count();
                if($count1>0){
                    Db::table('up_comment_reply')->where($where1)->delete();
                }
            }
            Db::table('up_comment')->where($where)->delete();

        }
        $res = Db::table('up_media')->where('media_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    //添加
    public function add(){
        if(request()->isPost()){
            $data['media_title'] = input('param.information_title');
            $data['media_content'] = input('param.content');
            $data['is_admin'] = 2;//1前端   2后台
            $data['media_url'] = input('param.photo');//图片
            $data['media_time'] = date('Y-m-d H:i:s',time());
            $data['media_update_time'] = date('Y-m-d H:i:s',time());
            $data['media_user_id'] = session('uid');
            $data['media_cz_user'] = session('uid');
            $data['media_url_thumb'] = explode(',',$data['media_url'])[0];
            $res = Db::table('up_media')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    //编辑
    public function edit(){
        if(request()->isPost()){
            $id = input('param.id');
            $data['media_title'] = input('param.information_title');
            $data['media_content'] = input('param.content');
            $data['media_url'] = input('param.photo');//图片
            $data['media_update_time'] = date('Y-m-d H:i:s',time());
            $data['media_cz_user'] = session('uid');
            $data['media_url_thumb'] = explode(',',$data['media_url'])[0];
            $res = Db::table('up_media')->where('media_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }

        $id = input('param.id');
        $info = Db::table('up_media')->where('media_id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function detail()
    {
        $id = input('param.id');
        $list = Db::table('up_media')->alias('a')
            ->join('up_admin ua','ua.id = a.media_cz_user','left')
            ->where('media_id',$id)
            ->field('a.*,ua.username')
            ->find();
        if($list['is_admin']==1){//前台用户发布
            $user_username1 = Db::table('up_u_user')->where('user_id',$list['media_user_id'])->value('user_username');
        }elseif ($list['is_admin']==2){//后台发布
            $user_username1 = Db::table('up_admin')->where('id',$list['media_user_id'])->value('username');
        }
        $list['user_username1'] = $user_username1;
        $this->assign('list',$list);
        return $this->fetch();
    }
}